﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace thinger.DelegateLambda
{
    public class ActionAndFuncBase
    {
        public void Test1()
        {
            Action<string> action1 = (name) => Console.WriteLine(name);
            Action<string, int> action2 = (name, age) => Console.WriteLine(name + " " + age);
            action1.Invoke("雪球");
            action2.Invoke("来福", 3);

            Func<string, string> func1 = (name) => name;
            Func<string, int, string> func2 = (name, age) => name + " " + age;

            func1.Invoke("小雪球");
            func2.Invoke("小来福", 3);
        }


        #region Func与多态特性的结合重要应用（高级应用）

        public int operation(Func<int,int,int> func,int[] nums,int index,int count)
        {
            int result = nums[index];  //把第一个值作为一个基数  
            for (int i = index + 1; i < index + count; i++)
            {
                result = func(result, nums[i]);
            }
            return result;
        }

        public int Add(int a,int b)
        {
            return a + b;
        }


        public void Test2()
        {
            int[] nums = { 10, 9, 8, 7, 6, 5, 4, 3, 2 };

            int result = operation(Add, nums, 0, 4);
            Console.WriteLine("10,9, 8, 7的和：" + result);

            int result2 = operation((a, b) => a + b, nums, 0, 4);
            Console.WriteLine("10,9, 8, 7的和：" + result2);
        }


        #endregion
    }
}
